package mao.leetcode.others.leetcode110;

public class Solution {
    private boolean flag= true; //公共变量，用于保存信息。
    public boolean isBalanced(TreeNode root) {
        isBalanced_helper(root);
        return flag;
    }

    public int isBalanced_helper(TreeNode root) {
        if (!flag) return 0;
        if (root == null) return 0;
        int dept_left = isBalanced_helper(root.left);
        int dept_right = isBalanced_helper(root.right);
        if (Math.abs(dept_left-dept_right) > 1)  flag=false;
        return Math.max(dept_left,dept_right)+1;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
